package com.yonyou.crm.bpub.approval.util;

import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;

import com.yonyou.crm.bpub.itf.IProcessService;

public class ProcessObtainUtil{
	/**
	 * 根据单据类型获取对应的接口
	 * @param djType
	 * @param applicationContext
	 * @return
	 */
	public  IProcessService getProcessService(int djType,ApplicationContext applicationContext) {
		ProcessObjectInfo info = ProcessObjectInfoFactory.getInstance().getInfo(String.valueOf(djType));
		String serviceName = info.getClassName();
		ConfigurableApplicationContext context = (ConfigurableApplicationContext) applicationContext;
		// Bean的实例工厂
		DefaultListableBeanFactory dbf = (DefaultListableBeanFactory) context
				.getBeanFactory();
		// Bean构建 要创建的Bean的Class对象
		BeanDefinitionBuilder dataSourceBuider = BeanDefinitionBuilder
				.genericBeanDefinition(serviceName);
		// 将实例注册spring容器中
		dbf.registerBeanDefinition(info.getBeanName(),
				dataSourceBuider.getBeanDefinition());
		// 获取bean
		IProcessService serviceImpl = (IProcessService) dbf
				.getBean(info.getBeanName());
		return serviceImpl;

	}
	
}
